<?php
namespace app\common\logic;

use app\common\exception\UserException;
use app\common\model\CapitalWater as CapitalWaterModel;
use app\common\model\CapitalWater;
use app\common\model\UserBonusDetail;
use app\common\model\UserRole;
use think\Controller;
use app\common\model\Capital as CapitalModel;
use think\Db;

class Capital extends Controller
{
    /**
     * @param $userid
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取用户资金
     */
    public function get_user_capital($userid){
        return CapitalModel::where("uid",$userid)->where("type",2)->where("status",1)->find();
    }

    /**
     * @param $userid
     * @param int $start
     * @param int $num
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取用户钱包
     */
    public function get_user_capital_data($userid,$usertype){


        switch ($usertype){
            case 1:   //会员

                $userdata=\app\common\model\User::where("id",$userid)->where("status",1)->find();
                if(!$userdata){
                    throw new UserException(["code"=>201,"msg"=>"用户被冻结或不存在","errorCode"=>300]);
                }

                $data= CapitalModel::where("uid",$userid)->where("type",2)->where("status",1)->field("money,bonus_money,put_out_money,recommend_money,putmoney,shareholder_money,count_putmoney,count_packet_money,agent_money")->find();
               
                $data['recharge_hongbao']= CapitalWater::where("uid",$userid)->where("type",'in',[7,12])->sum("money");
                $data['put_out_money']=floor($data['put_out_money']*100+$data['recharge_hongbao']*100)/100;
                $data['pay_hongbao']= CapitalWater::where("uid",$userid)->where("type",'in',[5,8])->sum("money");
                $data['zr_money']=0;
                $data['sy_money']=$data['shareholder_money']-$data['zr_money'];
                if($userdata['shop_id']){
                    $data['shopname']= \app\common\model\Shop::where("id",$userdata['shop_id'])->value("name");
                    $data['shopid']= $userdata['shop_id'];
                }
                $yesday=strtotime(date("Y-m-d",strtotime("-1 day")));
                $today=strtotime(date("Y-m-d",time()));
                $where=[];
                $where["addtime"]=['between',[$yesday,$today]];
                $where2=[];
                $where2["bonus_time"]=['between',[date("Y-m-d",strtotime("-1 day")),date("Y-m-d",time())]];
                $y_recharge_hongbao= CapitalWater::where("uid",$userid)->where($where)->where("type",7)->sum("money");  //充值
                $y_pay_hongbao= CapitalWater::where("uid",$userid)->where($where)->where("type",8)->sum("money");     //消费
                $y_yichu_hongbao= CapitalWater::where("uid",$userid)->where($where)->where("type",10)->sum("money");  //溢出
                $y_tj_hongbao= CapitalWater::where("uid",$userid)->where($where)->where("type",9)->sum("money");  //推荐奖
                $y_gd_hongbao= UserBonusDetail::where("user_id",$userid)->where($where2)->sum("bonus_money");  //股东分红奖
                $data['yesterday_hongbao']=$y_recharge_hongbao+$y_pay_hongbao+$y_yichu_hongbao+$y_tj_hongbao+$y_gd_hongbao;
                $yichu_hongbao= CapitalWater::where("uid",$userid)->where("type",10)->sum("money");  //溢出
                $data['y_yichu_hongbao']= $yichu_hongbao;
                return $data;
                break;
            case 2:  //商家
                $data= CapitalModel::where("uid",$userid)->where("type",1)->where("status",1)->field("put_out_money,putmoney,count_putmoney")->find();
                $shopdata=\app\common\model\Shop::where("id",$userid)->where("status",1)->field("surplus,total")->find();
                $where=[];
                $where["created_at"]=['between',[date("Y-m-d",strtotime("-1 day")),date("Y-m-d",time())]];
                $data['ys_income']=Db::table("t_shop_profit")->where("shop_id",$userid)->where($where)->sum("money");
                $data['surplus']=$shopdata['surplus'];
                $data['total']=$shopdata['total'];

                return $data;
                break;
            case 3:  //代理商
                $data= Db::table("t_agent")->where("id",$userid)->where("status",1)->field("money")->find();
                if(!$data){
                    throw new UserException(["code"=>201,"msg"=>"用户被冻结或不存在","errorCode"=>300]);
                }
                $yesday=strtotime(date("Y-m-d",strtotime("-1 day")));
                $today=strtotime(date("Y-m-d",time()));
                $where=[];
                $where["addtime"]=['between',[$yesday,$today]];
                $data['ys_income']=Db::table("t_agent_log")->where("aid",$userid)->where($where)->sum("money");
                return $data;
                break;
            case 4: //省代理
                $data= Db::table("t_province_agent")->where("id",$userid)->where("status",1)->field("money")->find();
                if(!$data){
                    throw new UserException(["code"=>201,"msg"=>"用户被冻结或不存在","errorCode"=>300]);
                }
                $yesday=strtotime(date("Y-m-d",strtotime("-1 day")));
                $today=strtotime(date("Y-m-d",time()));
                $where=[];
                $where["addtime"]=['between',[$yesday,$today]];
                $data['ys_income']=Db::table("t_province_agent_log")->where("aid",$userid)->where($where)->sum("money");
                return $data;
                break;
            case 5: //内部员工
                $data= Db::table("t_real_bonus_member")->where("id",$userid)->where("status",1)->field("money")->find();
                if(!$data){
                    throw new UserException(["code"=>201,"msg"=>"用户被冻结或不存在","errorCode"=>300]);
                }
                $where=[];
                $where["created_at"]=['between',[date("Y-m-d",strtotime("-1 day")),date("Y-m-d",time())]];
                $data['ys_income']=Db::table("t_member_bonus_detail")->where("member_id",$userid)->where($where)->sum("bonus_money");
                return $data;
                break;
            default :
                throw new UserException(["code"=>201,"msg"=>"非法操作","errorCode"=>300]);
                break;

        }

    }


    public function add_user_capitallist($userid){
        $capital=CapitalModel::create([
            "type" => 2,
            "uid" => $userid
        ]);
    }

    public function update_capital($userid,$rid){
        $new_putmoney=db("user_role")->where("id",$rid)->value("new_putmoney");
        if($rid==3){
            $new_putmoney=0;
        }
        $sc=CapitalModel::where("uid",$userid)->where("status",1)->where("type",2)->update([
            "putmoney"=>$new_putmoney
        ]);
        return $sc;
    }
}